#include <math.h>
#include "BaseD3D.h"
#include "GlobalUse.h"
#include "Sprite.h"

VBaseD3D::VBaseD3D(void)
{
}

VBaseD3D::~VBaseD3D(void)
{
    sprite->Release();
    d3ddev->Release();
    d3d->Release();
}

// this function initializes and prepares Direct3D for use
void VBaseD3D::InitD3D()
{
	HWND hWnd = gfGetMainWin();

	//Initialize d3d
    d3d = Direct3DCreate9(D3D_SDK_VERSION);					//Use SDK

    D3DPRESENT_PARAMETERS d3dpp;							//D3D Present Parameter

    ZeroMemory(&d3dpp, sizeof(d3dpp));						//Empty memory
    d3dpp.Windowed = TRUE;									//This PP is windowed
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;				//Allow buffering
    d3dpp.hDeviceWindow = hWnd;							//Using HWND as Device Window
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;				//Back buffer draw using 8-BIT
    d3dpp.BackBufferWidth = SCREEN_WIDTH;					//The width of back buffer
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;					//The height of back buffer
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;


    // create a device class using this information and the info from the d3dpp stuct
    d3d->CreateDevice(D3DADAPTER_DEFAULT,					//Create D3D Device using default adapter
                      D3DDEVTYPE_HAL,						//Huh?
                      hWnd,									//The D3D device will use HWND as Device Window
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,	//Process will follow vertex?
                      &d3dpp,								//Using PP as address
                      &d3ddev);								//and load to d3d device

	//Initialize d3dspt
    D3DXCreateSprite(d3ddev, &d3dspt);			// create the Direct3D Sprite object

	sprite = new VSprite(_T("panel2.png"));
	text = new VText(_T("Hello World!!"));
	animation = new VAnimatedSprite(_T("panel3.png"));
	animation->SetPosition(200,200);
	animation->SetDrawSize(182,128);
	animation->SetMaximumAnimation(21);
	animation->SetMaximumTime(3);
	animation->StopAnimation();
	
	basicModel = new VBasicModel();
	basicModel->Init();

	camera = new VCamera();
	camera->Init();

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting
    d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);    // turn off culling
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer

    return;
}
//Zeta78695
	Delta86531

// this is the function used to render a single frame
void VBaseD3D::RenderFrame(void)
{
    // clear the window to a deep blue
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();									// begins the 3D scene

    d3ddev->SetFVF(CUSTOMFVF);

    d3dspt->Begin(D3DXSPRITE_ALPHABLEND);					// begin sprite drawing

	//sprite->Draw();											// draw the sprite

	//animation->Draw();

	text->Draw();

    d3dspt->End();											// end sprite drawing

	basicModel->Draw();

    d3ddev->EndScene();										// ends the 3D scene

    d3ddev->Present(NULL, NULL, NULL, NULL);

    return;
}

void VBaseD3D::CheckInput(RAWINPUT rInput) {
    if (rInput.header.dwType == RIM_TYPEKEYBOARD) 
    {
		int nCurAnim = animation->GetCurrentAnimation();
		switch (rInput.data.keyboard.VKey) {
			case VK_W:
				camera->MoveForward(0.1);
				camera->Update();
				break;
			case VK_S:
				camera->MoveBackward(0.1);
				camera->Update();
				break;
			case VK_A:
				camera->MoveLeft(0.1);
				camera->Update();
				break;
			case VK_D:
				camera->MoveRight(0.1);
				camera->Update();
				break;
			default:
				break;
		}
	}
	else if (rInput.header.dwType == RIM_TYPEMOUSE) {
		camera->RotateX((float)rInput.data.mouse.lLastX/100);
		camera->RotateY(-(float)rInput.data.mouse.lLastY/100);
		camera->Update();

		SetCursorPos(SCREEN_WIDTH/2,SCREEN_HEIGHT/2);
	}
	CString abc;
	abc.Format(_T("X: %.0f, Y: %.0f, Z:%.0f\nX: %.0f, Y: %0f, Z:%.0f"),camera->GetAngleX(),camera->GetAngleY(),camera->GetAngleZ(),camera->m_v3Position.x,camera->m_v3Position.y,camera->m_v3Position.z);
	text->SetText(abc);
}
